import { isValid } from "cc";

export class CustomEvent {
  /**存储 */
  private list: any = {};
  /**
   *  注册消息
   * @param key key
   * @param func 回调
   * @param type 区别其他key值相同的注册
   */
  on(key: string, func: Function, type: string = "self") {
    if (this.list[key] == null) {
      this.list[key] = [];
    }
    let d: any = {};
    d.func = func;
    d.type = type;
    this.list[key].push(d);
  }
  /**
   * 移除一个消息
   */
  remove(key: string, type: string = "self") {
    if (this.list[key]) {
      let data = this.list[key];
      for (let i = 0; i < data.length; i++) {
        if (data[i].type == type) {
          data.splice(i, 1);
          --i;
        }
      }
    }
  }
  /**
   * 移除type相同的所有消息
   */
  removeByNode(type: string) {
    for (let p in this.list) {
      let data = this.list[p];
      for (let i = 0; i < data.length; i++) {
        if (data[i].type == type) {
          data.splice(i, 1);
          --i;
        }
      }
    }
  }
  /**
   * 移除key注册的所有消息
   */
  removeByKey(key: string) {
    delete this.list[key];
  }
  /**
   * 派发消息
   */
  emit(key: string, arg?: any) {
    if (this.list[key]) {
      let funcs = this.list[key].concat();
      for (let i = 0; i < funcs.length; i++) {
        if (funcs[i].type && isValid(funcs[i].type)) {
          funcs[i].func(arg, funcs[i].type);
        }
      }
    }
  }
}
